﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic.Products;
using Module.Core.Common;

namespace Web
{
    public partial class _default : System.Web.UI.Page
    {
        protected float Price_vat;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                dt = ProductsBL.GetDataLatest(15, 0, 1, ClsCheckBox.SanPhamNoiBat);
                if (dt != null && dt.Rows.Count > 0)
                {                    
                    rptProduct.DataSource = dt;
                    rptProduct.ItemDataBound += new RepeaterItemEventHandler(rptProduct_ItemDataBound);
                    rptProduct.DataBind();                                    
                }

            }
            catch (Exception ex)
            {   
                Response.Write(ex.Message);             
            }
        }

        void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HtmlGenericControl divGia = (HtmlGenericControl)e.Item.FindControl("divGia");
            Label lbProductName = (Label) e.Item.FindControl("lbProductName");
            DataRowView dr = (DataRowView)e.Item.DataItem;
            if(dr["ProductName"] !=null && dr["ProductName"] !="")
            {
                if(dr["ProductName"].ToString().Length > 30)
                {
                    lbProductName.Text = dr["ProductName"].ToString().Substring(0, 30)+" ...";
                }
                else
                {
                    lbProductName.Text = dr["ProductName"].ToString();
                }
            }
            if (float.Parse(dr["Price_VAT"].ToString()) > 0 && float.Parse(dr["Price"].ToString()) > 0)
            {
                Price_vat = float.Parse(dr["Price"].ToString()) - ((float.Parse(dr["Price_VAT"].ToString()) * float.Parse(dr["Price"].ToString())) / 100);
            }
            else
            {
                Price_vat = float.Parse(dr["Price"].ToString());
            }
            Label lbgiaban = (Label) e.Item.FindControl("lbgiaban");
            lbgiaban.Text = "Giá bán: "+ String.Format("{0:0,0}",Price_vat) +" (VND)";
            if (float.Parse(dr["Price_VAT"].ToString()) > 0)
            {
                divGia.Visible = true;
            }
        }

        protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }
    }
}